package com.zh.tea.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.tea.dao.TeaMapper;
import com.zh.tea.entity.PageTea;
import com.zh.tea.entity.Tea;
import com.zh.tea.entity.TeaExample;
import com.zh.tea.service.TeaService;
import com.zh.tea.utils.SessionFactoryUtils;

public class TeaServiceImpl implements TeaService {
		
	/**
	 * 添加茶品
	 * author:006
	 */
	@Override
	public int addTea(Tea tea) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper teamapper=session.getMapper(TeaMapper.class);
		int n=teamapper.insert(tea);
		session.close();
		return n;
	}
	/**
	 * 查询茶品
	 */

	@Override
	public PageInfo<Tea> getTea(Tea tea,Integer pagNo,Integer pageSize) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper teamapper=session.getMapper(TeaMapper.class);
		TeaExample teaexm=new TeaExample();
		TeaExample.Criteria criteria=teaexm.createCriteria();
		if(tea!=null){
		if(tea.getTypeid()!=0){
			criteria.andTypeidEqualTo(tea.getTypeid());
		}
		}
		PageHelper.startPage(pagNo, pageSize);
		List<Tea> listt=teamapper.selectByExample(teaexm);
		System.out.println(listt.isEmpty());
		PageInfo<Tea> pagetea=new PageInfo<>(listt); 
		session.close();
		return pagetea;
	}
	//按照id查询 tea
	public Tea getTeaById(Integer id){
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		Tea tea=team.selectByPrimaryKey(id);
		session.close();
		
		return tea;
	}
	//按照teaid删除茶的信息
	@Override
	public int delTea(Tea tea) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		int n= team.deleteByPrimaryKey(tea.getId());
		session.close();
		return n;
	}

	//按照teaid 更新tea的信息
	@Override
	public int updTea(Tea tea) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		TeaMapper team=session.getMapper(TeaMapper.class);
		TeaExample teaexm=new TeaExample();
		TeaExample.Criteria criteria=teaexm.createCriteria();
		criteria.andIdEqualTo(tea.getId());
		Tea ntea=new Tea();
		//需要更改的信息
		if(tea.getBrandid()!=null){
		ntea.setBrandid(tea.getBrandid());
		}
		if(tea.getBzq()!=null){
			ntea.setBzq(tea.getBzq());
		}
		if(tea.getLicenseno()!=null){
			ntea.setLicenseno(tea.getLicenseno());
		}
		if(tea.getName()!=null){
			ntea.setName(tea.getName());
		}
		if(tea.getPackspec()!=null){
			ntea.setPackspec(tea.getPackspec());
		}
		if(tea.getPic1()!=null){
			ntea.setPic1(tea.getPic1());
		}
		if(tea.getPrice()!=null){
			ntea.setPrice(tea.getPrice());
		}
		if(tea.getProducedate()!=null){
			ntea.setProducedate(tea.getProducedate());
		}
		if(tea.getProloc()!=null){
			ntea.setProloc(tea.getProloc());
		}
		if(tea.getTypeid()!=null){
			ntea.setTypeid(tea.getTypeid());
		}
		int n= team.updateByExample(ntea, teaexm);
		session.close();
		return n;
	}
	



}
